﻿#include "computerinfowidget.h"
#include <QStyleOption>
#include <QPainter>
#include <QLabel>
#include <QPushButton>
#include "computerwindget.h"
#include "computerdisplaywidget.h"


#define COMPUTER_INFO_WIDGET_WIDTH 210
#define COMPUTER_INFO_WIDGET_HEIGHT 160

ComputerInfoWidget::ComputerInfoWidget(Computer &computer, QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    connect(ui.editPushButton, SIGNAL(clicked(bool)), this, SLOT(onEditeComputerClicked()));
    connect(ui.displayPushButton, SIGNAL(clicked(bool)),this, SLOT(onShowComputerInfoClicked()));

    this->setMinimumSize(COMPUTER_INFO_WIDGET_WIDTH,COMPUTER_INFO_WIDGET_HEIGHT);
    this->setMaximumSize(COMPUTER_INFO_WIDGET_WIDTH,COMPUTER_INFO_WIDGET_HEIGHT);

    setComputer(computer);

}

void ComputerInfoWidget::setComputer(Computer &computer)
{

    this->computer = computer;

    ui.nameLabel->setText(this->computer.name);

    if(this->computer.isOffLine())
    {
        ui.statusImageWidget->setStyleSheet("QWidget{border-image: url(:/new/icon/ui/computer_bad.png);}");
    }
    else if(this->computer.unReadALarmCount > 0)
    {
        ui.statusImageWidget->setStyleSheet("QWidget{border-image: url(:/new/icon/ui/computer_warn.png);}");
    }
    else
    {
        ui.statusImageWidget->setStyleSheet("QWidget{border-image: url(:/new/icon/ui/computer_good.png);}");
    }

    ui.statusTextEdit->setPlainText("ip:" + this->computer.ip + "\n连接状态:" + this->computer.getOffLineState() + "\n有未读异常:"
                                    + int2QStr(this->computer.unReadALarmCount) + "条");

}




void ComputerInfoWidget::paintEvent(QPaintEvent *)
{
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void ComputerInfoWidget::onEditeComputerClicked()
{
    ComputerWindget * win = new ComputerWindget;
    win->setModal(false);;
    win->prepareUpdate(computer);
    win->show();
}

void ComputerInfoWidget::onShowComputerInfoClicked()
{

    ComputerDisplayWidget * win = new ComputerDisplayWidget(computer,this);
    win->setModal(false);;
    win->show();
//    waitForDelayConnect(1);
//    emit this->delayGetClientAndAppInfo();
}

void ComputerInfoWidget::waitForDelayConnect(unsigned int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
    QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
